package vn.info.vietnaminfo.adapter;

import java.util.List;

import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.RetailerActivity;
import vn.info.vietnaminfo.VietNamInfoActivity;
import vn.info.vietnaminfo.data.DSLoaiHinhKD;
import vn.info.vietnaminfo.dialog.ShowDialog;
import vn.info.vietnaminfo.utli.GlobalVariable;
import vn.info.vietnaminfo.utli.HttpUtil;
import vn.info.vietnaminfo.utli.Stuff;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LoaiHinhKDAdapter extends BaseAdapter
{
	private Context mContext;
	private LayoutInflater mInflater;	
	private Drawable[] ARRAY_IMAGE = null;
	private List<DSLoaiHinhKD> listLoaiHinhKD = null;
	
	public LoaiHinhKDAdapter(Context context , Drawable[] image, List<DSLoaiHinhKD> object)
	{
		mContext  		= context;
		listLoaiHinhKD 	= object;
		ARRAY_IMAGE		= image;
		mInflater 		= LayoutInflater.from(context);
	}
	
	public class ViewHolder
	{
		ImageView iconImage;
		TextView iconText;
	}

	public View getView(final int position, View view, ViewGroup parent)
	{
		final ViewHolder holder;
		if (view == null)
		{
			holder 				= new ViewHolder();
			view 				= mInflater.inflate(R.layout.layout_home_icon, null);
			holder.iconImage 	= (ImageView)view.findViewById(R.id.icon_image);
			holder.iconText		= (TextView)view.findViewById(R.id.icon_text);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) view.getTag();
		}
		holder.iconImage.setTag(position);
		holder.iconText.setTag(position);
		
		holder.iconText.setText(listLoaiHinhKD.get(position).getTenLoaiKD());
		if(ARRAY_IMAGE[position] != null)
		{
			holder.iconImage.setBackgroundDrawable(ARRAY_IMAGE[position]);
		}
		
		holder.iconImage.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				ShowDialog show = new ShowDialog(mContext);
				
				// Kiểm tra kết nối mạng
				if(!Stuff.isConnected((mContext)))
				{
					show.ThongBaoCaiDatMang();
					return;
				}
				
				//su dung ban do
				Intent intent = new Intent(mContext, RetailerActivity.class);
				((VietNamInfoActivity)mContext).startActivityForResult(intent, 500);
				((Activity) mContext).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}
		});
		return view;
	}
	
	public int getCount()
	{
		return listLoaiHinhKD.size();
	}

	public Object getItem(int position)
	{
		return position;
	}

	public long getItemId(int position)
	{
		return position;
	}
}